package com.woniuxy.ticketorder.controller;

import com.woniuxy.commnes.entity.ResponseResult;
import com.woniuxy.ticketorder.entity.TicketOrder;
import com.woniuxy.ticketorder.entity.dto.OrderDto;
import com.woniuxy.ticketorder.service.CinemaService;
import com.woniuxy.ticketorder.service.OrderService;
import com.woniuxy.ticketorder.service.UserService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @ClassName OrderController
 * @Description TODO
 * @Author zx
 * @Date 2021-02-23 14:17:09
 * @Version 1.0
 */
@Validated
@Api(tags = "订单管理系统")
@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {

    @Resource
    private RestTemplate restTemplate;


    @GetMapping("add")
    public String add(){
        //指定传的参数名
        String serviceUser = "http://ticket-user/user/add?name={1}";
        //多参数的话建议使用map
//        Map<String,Object> paramMap=new HashMap<>();
//        //写入参数KV形式
//        paramMap.put("name","小明");
        ResponseEntity<String> forEntity = restTemplate.getForEntity(serviceUser, String.class,"小红");
        System.out.println(forEntity.getBody());
        //最原始写法
//        try {
//            URL url = new URL(serviceUser);
//            URLConnection urlConnection = null;
//            urlConnection = url.openConnection();
//            InputStream inputStream = urlConnection.getInputStream();
//            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
//            log.debug(reader.readLine());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return "order_add";
    }

    //Spring Cloud OpenFeign
    @Resource
    private UserService userService;


    @ApiOperation("添加订单的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名"),
            @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "深圳", required = true)
    }
    )
    @GetMapping("add2")
    public String add2(String name){
        return userService.add(name);
    }

    @ApiOperation("添加地址")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "1"),
//            @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "深圳", required = true)
//    }
//    )
    @PostMapping("update")
    public String update(@RequestBody TicketOrder order){
        return "你好";
    }


    @Resource
    private CinemaService cinemaService;

    @Resource
    private OrderService orderService;

    /**
     * 添加订单
     * @param orderDto
     * @return
     */
    @GlobalTransactional
    @Transactional
    @PostMapping("addOrder")
//    @HystrixCommand(fallbackMethod = "acb")
    public ResponseResult<?> addOrder(@RequestBody @Valid OrderDto orderDto){
        System.out.println();
        //给cinema服务发送数据
        ResponseResult<Double> add = cinemaService.add(orderDto.getCinema(), orderDto.getHall(), orderDto.getSeat());

//        System.err.println(add.getData());//ResponseResult(code=200, msg=购票成功, data=57.2)
        //如果座位状态可修改则 返回购票成功(座位状态已改)  那么继续生成订单号
        if (add.getMsg().equals("购票成功")){
            orderDto.setMoney(add.getData());
            //生成订单(后期可以返回用户id)
            if (orderService.addOrder(orderDto)){
                System.out.println();
                //用户加积分 integration为多少积分，id为用户id
                    userService.addIntegration(50,1);
            }
        }else {
            return new ResponseResult<String>(200,add.getMsg());
        }
        return new ResponseResult<String>(200,add.getMsg());
    }


    @Value("${account}")
    private String account;

    @PostMapping("testConfig")
    public ResponseResult<String> test(){
        return new ResponseResult<>(200,"account为"+account);
    }
}
